﻿<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>微信机器人</title>
    <style>

        * {
            margin: 0;
            padding: 0;
            font-size: 16px;
        }

        .main {
            width: 280px;
            height: 400px;
            background: #F5F5F5;
            margin: 0 auto;
            margin-top: calc(50vh - 200px);
            border-radius: 2px;
            box-shadow: 0 0 10px 2px rgba(0, 0, 0, 0.2);
        }

        .title {
            padding: 8px;
        }

        .title > span {
            float: left;
            width: 200px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }

        .title > a {
            float: right;
            color: #C50000;
            cursor: pointer;
        }

        .title > a:hover {
            color: #FF0000;
        }

        .status {
            width: 192px;
            height: 192px;
            margin: 0 auto;
            margin-top: 50px;
        }

        .status img {
            width: 100%;
            height: 100%;
        }

        .tip {
            width: 240px;
            margin: 0 auto;
            margin-top: 12px;
            color: #808080;
            text-align: center;
        }

        .tip p {
            font-size: 14px;
        }

        .info {
            width: 240px;
            margin: 0 auto;
            margin-top: 12px;
            color: #808080;
            text-align: center;
        }

        .info p {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-size: 14px;
        }

        #other {
            top: 0;
            left: calc(50% - 140px);
            position: absolute;
            display: none;
        }

    </style>
</head>

<body>

<div class="main">
    <div class="title">
        <span>微信机器人</span> <a id="reboot" onclick="reboot()">重启</a>
        <div style="clear:both"></div>
    </div>
    <div class="status">
        <img src="" id="status"/>
    </div>
    <div class="info" id="info">
        <div class="tip" style="color:#ff0000">
            <p>不管出现什么问题</p>
            <p>请点击右上的重启按钮试试</p>
        </div>
    </div>
    <input type="hidden" id="robot_id" value="">
    <div class="main" id="other">维护中。。。</div>
</div>
<script>
    function base64encode(str) {
        var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var out, i, len;
        var c1, c2, c3;
        len = str.length;
        i = 0;
        out = "";
        while (i < len) {
            c1 = str.charCodeAt(i++) & 0xff;
            if (i == len) {
                out += base64EncodeChars.charAt(c1 >> 2);
                out += base64EncodeChars.charAt((c1 & 0x3) << 4);
                out += "==";
                break;
            }
            c2 = str.charCodeAt(i++);
            if (i == len) {
                out += base64EncodeChars.charAt(c1 >> 2);
                out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                out += base64EncodeChars.charAt((c2 & 0xF) << 2);
                out += "=";
                break;
            }
            c3 = str.charCodeAt(i++);
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
            out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    }

    function html_encode(s) {
        var el = document.createElement('div');
        el.innerText = el.textContent = s;
        return el.innerHTML;
    }

    function status_loading() {
        document.getElementById("status").src = "";
        document.getElementById("info").innerHTML = "初始化...";
    }

    function status_qrcode(data, robot_id) {
        document.getElementById("status").src = data;
        document.getElementById("info").innerHTML = "扫码登陆";
        document.getElementById("robot_id").value = robot_id;
    }

    function status_error() {
        document.getElementById("status").src = "";
        document.getElementById("info").innerHTML = "出现异常";
    }

    function status_ok(str, img) {
        if ("" !== img) document.getElementById("status").src = "data:image/jpeg;base64," + img;
        else document.getElementById("status").src = "";
        document.getElementById("info").innerHTML = str;
    }

    status_loading();

</script>
<script>
    var robot_id = document.getElementById("robot_id").value = '';

    function reboot() {
        // document.getElementById("reboot").setAttribute("disabled", "disabled");
        //
        // var xhr = new XMLHttpRequest();
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4) {
        //         document.getElementById("reboot").removeAttribute("disabled");
        //     }
        // }
        //
        // xhr.open("POST", "/api/index/");
        // xhr.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
        // xhr.send("robot_id="+ encodeURIComponent(robot_id) + "&type=1");

        document.getElementById("reboot").setAttribute("disabled", "disabled");
        robot_id = document.getElementById("robot_id").value = '';
        status_loading();
    }

    function get() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (200 == xhr.status) {
                    data = JSON.parse(xhr.responseText);
                    console.log(data)
                    if (data.code == 200) {
                        document.getElementById("other").style.display = data.service == 0 ? "block" : "none";
                        if (data.data.status == 0) {
                            document.getElementById("robot_id").value = '';
                            status_loading();
                        }
                        else if (data.data.status == 1) {
                            robot_id = data.data.robot_id;
                            status_qrcode("data:image/png;base64," + data.data.wx_qrcode, data.data.robot_id);
                        }
                        else if (data.data.status == 2) {
                            status_ok('<p>' + html_encode(data.data.wx_name) + '</p>' + '<p>微信签名：' + html_encode(data.data.wx_signature) + '</p>', data.data.wx_logo);
                        }
                    } else if (201 == xhr.status) {
                        document.getElementById("info").innerHTML = "请扫码登陆！！！";
                    } else {
                        status_error();
                    }
                }
                setTimeout(get, 10000);
            }
        }

        xhr.open("POST", "/api/index/");
        xhr.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
        xhr.send("robot_id=" + encodeURIComponent(robot_id) + "&type=1");
    }

    get();
</script>
</body>
</html>
